package de.koller.worlders.engine.entity.plattform;

import de.koller.oceanFramework.math.Vec;
import de.koller.worlders.engine.entity.mo.MovingObject;
import de.koller.worlders.engine.tiles.ITile;
import de.koller.worlders.engine.tiles.data.ITileCreator;
import de.koller.worlders.render.VisualMap;
import de.koller.worlders.render.visual.DisplayRenderObject;

public class SolidPlattform extends PlattformEffect {
	public static final int GEN_ID = 1;
	
	public static final ITileCreator GEN_INSTANCE = new ITileCreator() {
		@Override public ITile newInstance() {
			return new Plattform().addEffect( new SolidPlattform( SolidType.Wall ) );
		}
		
		@Override public DisplayRenderObject getIcon() { return VisualMap.solidWall; }
		@Override public String getName() { return "Mauer"; }
	};

	public static final ITileCreator GEN_INSTANCE_1 = new ITileCreator() {
		@Override public ITile newInstance() {
			return new Plattform().addEffect( new SolidPlattform( SolidType.Grass ) );
		}
		
		@Override public DisplayRenderObject getIcon() { return VisualMap.solidGrass; }
		@Override public String getName() { return "Gras"; }
	};
	
	public static final ITileCreator GEN_INSTANCE_2 = new ITileCreator() {
		@Override public ITile newInstance() {
			return new Plattform().addEffect( new SolidPlattform( SolidType.Dirt ) );
		}
		
		@Override public DisplayRenderObject getIcon() { return VisualMap.solidDirt; }
		@Override public String getName() { return "Erde"; }
	};
	
	public static final ITileCreator GEN_INSTANCE_3 = new ITileCreator() {
		@Override public ITile newInstance() {
			return new Plattform().addEffect( new SolidPlattform( SolidType.PurpleRock ) );
		}
		
		@Override public DisplayRenderObject getIcon() { return VisualMap.solidPurp; }
		@Override public String getName() { return "Lila Fels"; }
	};
	
	public static final ITileCreator GEN_INSTANCE_4 = new ITileCreator() {
		@Override public ITile newInstance() {
			return new Plattform().addEffect( new SolidPlattform( SolidType.White ) );
		}
		
		@Override public DisplayRenderObject getIcon() { return VisualMap.solidWhite; }
		@Override public String getName() { return "Weißer Stein"; }
	};
	
	private final SolidType type;
	
	public SolidPlattform( SolidType type ) {
		this.type = type;
	}
	
	@Override
	public void init( Plattform p ) {
		super.init( p );
		
//		if( type == SolidType.Wall ) p.setTextureRep( wallT );
//		if( type == SolidType.Grass ) p.setTextureRep( grassT );
//		if( type == SolidType.Dirt ) p.setTextureRep( dirtT );
//		if( type == SolidType.PurpleRock ) p.setTextureRep( purpT );
//		if( type == SolidType.White ) p.setTextureRep( whiteT );
		
		if( type == SolidType.Wall ) p.getTile().setVisual( VisualMap.solidWall );
		if( type == SolidType.Grass ) p.getTile().setVisual( VisualMap.solidGrass );
		if( type == SolidType.Dirt ) p.getTile().setVisual( VisualMap.solidDirt );
		if( type == SolidType.PurpleRock ) p.getTile().setVisual( VisualMap.solidPurp );
		if( type == SolidType.White ) p.getTile().setVisual( VisualMap.solidWhite );
		
		p.setPriority( 0 );
	}

	@Override
	public void collision( Vec wayBefore, Vec wayAfter, MovingObject mo ) {
		super.collision( wayBefore, wayAfter, mo );
		
		mo.setBottomContact( false );
		if( plattform.isAbove( mo ) || plattform.isBelow( mo ) ) {
			wayAfter.setY( 0 );
			mo.getEnergy().setY( 0 );
		} else {
			wayAfter.setX( 0 );
		}
		
		//@todo geht nur weil erst x gemacht wird dann y
		if( plattform.isAbove( mo ) && mo.getForce().getY() > 0 )
			mo.setBottomContact( true );
		else if( plattform.isBelow( mo ) && mo.getForce().getY() < 0 )
			mo.setBottomContact( true );
		
		final double c = 0.001;
		//@todo achtung positionset!
		if( plattform.isAbove( mo ) )
			mo.getPosition().setY( plattform.getPosition().getY() - mo.getSize().getY() - c );
		if( plattform.isBelow( mo ) )
			mo.getPosition().setY( plattform.getPosition().getY() + plattform.getSize().getY() + c );
		if( plattform.isLeft( mo ) )
			mo.getPosition().setX( plattform.getPosition().getX() - mo.getSize().getX() - c );
		if( plattform.isRight( mo ) )
			mo.getPosition().setX( plattform.getPosition().getX() + plattform.getSize().getX() + c );
	}

	public SolidType getType() {
		return type;
	}
	
	public static enum SolidType {
		Wall, Grass, Dirt, PurpleRock, White;
	}
	
}
